package com.livew.framework.base.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.livew.framework.base.model.BaseModel;
import com.livew.framework.base.service.BaseService;
import com.livew.framework.exception.XTBusinessException;
import com.livew.framework.pojo.AjaxResult;
import com.livew.framework.utils.XTStringUtils;

public abstract class BaseController <T extends BaseModel>{

	protected abstract BaseService<T> getService();

	private String controllerRequestMappingValue = null;

	private String getControllerRequestMappingValue() {
		if (XTStringUtils.isBlank(controllerRequestMappingValue)) {
			RequestMapping rm = this.getClass().getAnnotation(RequestMapping.class);
			if (rm != null) {
				controllerRequestMappingValue = rm.value()[0];
			} else {
				controllerRequestMappingValue = this.getClass().getSimpleName();
			}
		}
		return controllerRequestMappingValue;
	}

	/**
	 * 跳转到列表页<br>
	 * 约定jsp页面的目录与controller的名称相同
	 * 
	 * @return
	 */
	@RequestMapping("list")
	public String list() {
		return getControllerRequestMappingValue() + "/list";
	}

	/**
	 * 页面编辑标识
	 */
	protected static String PAGE_FLAG = "page_flag";

	@RequestMapping(value = "detail/{id}", method = RequestMethod.GET)
	public String detail(@PathVariable("id") Long id, Model model) {
		model.addAttribute("id", id);
		return getControllerRequestMappingValue() + "/detail";
	}

	@RequestMapping(value = "edit/{id}", method = RequestMethod.GET)
	public String edit(@PathVariable("id") Long id, Model model) {
		model.addAttribute("id", id);
		return getControllerRequestMappingValue() + "/create";
	}

	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String create(Model model) {
		model.addAttribute("id", "");
		return getControllerRequestMappingValue() + "/create";
	}

	@RequestMapping(value = "findById/{id}", method = RequestMethod.GET)
	@ResponseBody
	public AjaxResult findById(@PathVariable("id") Long id) {
		if (id == null) {
			return AjaxResult.getResult(Boolean.FALSE, "参数错误");
		}
		T t = null;
		try {
			t = getService().getByPrimaryKey(id);
		} catch (XTBusinessException e) {
			e.printStackTrace();
			return AjaxResult.getResult(Boolean.FALSE, e.getMessage());
		}
		return AjaxResult.getResult(t);
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "del", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResult del(T t) {
		if (t == null) {
			return AjaxResult.getResult(Boolean.FALSE, "参数错误");
		}
		try {
			getService().delByPrimaryKey(t);
		} catch (XTBusinessException e) {
			e.printStackTrace();
			return AjaxResult.getResult(Boolean.FALSE, e.getMessage());
		}
		return AjaxResult.getResult(Boolean.TRUE, "删除成功");
	}

	/**
	 * 禁用或启用
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "updateStatus", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResult updateStatus(T t) {
		if (t == null) {
			return AjaxResult.getResult(Boolean.FALSE, "参数错误");
		}
		try {
			getService().updateStatus(t);
		} catch (XTBusinessException e) {
			e.printStackTrace();
			return AjaxResult.getResult(Boolean.FALSE, e.getMessage());
		}
		return AjaxResult.getResult(Boolean.TRUE, "操作成功");
	}

	/**
	 * 保存
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "save", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResult save(T t) {
		if (t == null) {
			return AjaxResult.getResult(Boolean.FALSE, "参数错误");
		}
		try {
			getService().save(t);
		} catch (XTBusinessException e) {
			e.printStackTrace();
			return AjaxResult.getResult(Boolean.FALSE, e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			return AjaxResult.getResult(Boolean.FALSE, "内部异常");
		}
		return AjaxResult.getResult(Boolean.TRUE, t.getId() + "");
	}

	/**
	 * 修改
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResult update(T t) {
		if (t == null || t.getId() == null) {
			return AjaxResult.getResult(Boolean.FALSE, "参数错误");
		}
		try {
			getService().updateByPrimaryKey(t);
		} catch (XTBusinessException e) {
			e.printStackTrace();
			return AjaxResult.getResult(Boolean.FALSE, e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			return AjaxResult.getResult(Boolean.FALSE, "内部异常");
		}
		return AjaxResult.getResult(Boolean.TRUE, t.getId() + "");
	}

	@InitBinder
	public void InitBinder(WebDataBinder binder) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		CustomDateEditor dateEditor = new CustomDateEditor(df, true);
		binder.registerCustomEditor(Date.class, dateEditor);
	}
}
